local que_qixing = fk.CreateSkill {
    name = "que_qixing"
}

Fk:loadTranslationTable {
    ["que_qixing"] = "祈星",
    [":que_qixing"] = "出牌阶段限两次，你可以随机展示牌堆中一张与本回合上次展示点数+1的牌，然后进行一次<a href='qiyuan_nb'>祈愿</a>。",

    ["qiyuan_nb"] = "随机执行以下一项，效果选择时仅你可见<br>" ..
        "<font color='red'>令一名角色失去1点体力；<br>" ..
        "<font color='red'>令一名角色增加1点体力上限；<br>" ..
        "<font color='red'>下一次使用牌时，额外结算1次；<br>" ..
        "<font color='red'>下一次受到伤害时，回复1点体力；<br>" ..
        "<font color='red'>从三个同拓展包角色中选择获得至多1个；<br>" ..
        "<font color='red'>获得1张【杀】，本回合可额外使用一张【杀】；<br>" ..
        "<font color='red'>视为对一名角色使用1张伤害类卡牌(无距离限制)；<br>" ..
        "<font color='red'>摸1张牌并弃置一名角色至多1张牌，若其没有牌，你摸1张牌；<br>" ..
        "<font color='red'>将牌堆底的1张牌交给一名角色，并移去其一张判定牌，若其没有判定牌，其额外获得牌堆顶的1张牌。",


    ["@que_qixing_damagedown"] = "受击回血",
    ["@que_qixing_useup"] = "额外结算",
}
local U = require "packages/utility/utility"

que_qixing:addEffect("active", {
    anim_type = "offensive",
    prompt = function(self)
        local mark = Self:getMark("que_qixing-turn")
        local num = mark
        if Self:getMark("que_qixing-turn") > 0 then
            return "祈星：是否展示一张点数为 " .. num + 1 .. " 的牌"
        else
            return "祈星：是否随机展示一张牌"
        end
    end,
    card_num = 0,
    card_filter = Util.FalseFunc,
    target_num = 0,
    can_use = function(self, player)
        return player:usedSkillTimes(self.name, Player.HistoryPhase) < 2
    end,
    on_use = function(self, room, effect)
        local player = effect.from
        local mark = player:getMark("que_qixing-turn")
        local c = math.random(1, 12)
        local skillName = que_qixing.name
        if player:getMark("que_qixing-turn") ~= 0 then
            c = mark
            if c == 13 then
                c = 0
            end
        end
        local cid = room:getCardsFromPileByRule(".|" .. c + 1, 1)
        player:showCards(cid)
        room:delay(800)
        room:setPlayerMark(player, "que_qixing-turn", Fk:getCardById(cid[1]).number)
        for _ = 1, 1 + player:getMark("que_zheyue_usetime"), 1 do
            if player:getMark("qiyue_skill_add") < 2 then
                local num = math.random(1, 8)
                local n = 1 + player:getMark("que_zheyue_num")
                local to
                if num == 2 or num == 4 or num == 1 or num == 8 or num == 6 then
                    local prompt = ""
                    if num == 2 then
                        prompt = "祈月：选择一名角色失去 " .. n .. " 点体力"
                    elseif num == 4 then
                        prompt = "祈月：选择一名角色增加 " .. n .. " 点体力上限"
                    elseif num == 1 then
                        prompt = "祈月：选择一名角色，弃置其 " .. n .. " 张牌，若其没有牌，你摸" .. n .. "张牌"
                    elseif num == 8 then
                        prompt = "祈月：选择一名角色，视为对其使用 " .. n .. " 张伤害牌"
                    elseif num == 6 then
                        prompt = "祈月：选择一名角色，令其获得牌堆的底的" .. n .. "张牌，然后弃置其一张判定牌，若没有判定牌，其额外获得牌堆的顶牌"
                    end
                    local tos = room:askToChoosePlayers(player, {
                        skill_name = skillName,
                        min_num = 1,
                        max_num = 1,
                        targets = room.alive_players,
                        prompt = prompt,
                        cancelable = true,
                    })
                    if #tos == 0 then return end
                    to = tos[1]
                end
                if player.dead then return end
                if num == 1 then
                    player:drawCards(n, skillName)
                    if #to:getCardIds { Player.Hand, Player.Equip, Player.Judge } > 0 then
                        local cards = room:askToChooseCards(player,
                            {
                                min = 1,
                                max = n,
                                flag = "he",
                                skill_name = skillName,
                                target = to,
                            }
                        )
                        room:throwCard(cards, skillName, to, player)
                    else
                        player:drawCards(n, skillName)
                    end
                elseif num == 2 then
                    room:loseHp(to, n, skillName)
                elseif num == 3 then
                    room:addPlayerMark(player, "@que_qixing_damagedown", 1)
                elseif num == 4 then
                    room:changeMaxHp(to, n)
                elseif num == 5 then
                    room:addPlayerMark(player, "@que_qixing_useup", 1)
                elseif num == 6 then
                    to:drawCards(n, skillName, "top")
                    if #to:getCardIds(Player.Judge) > 0 then
                        local cards = room:askToChooseCards(player,
                            {
                                min = 1,
                                max = n,
                                flag = "j",
                                skill_name = skillName,
                                target = to,
                            }
                        )
                        room:throwCard(cards, skillName, to, player)
                    else
                        to:drawCards(n, skillName, "bottom")
                    end
                elseif num == 7 then
                    local card = room:getCardsFromPileByRule("slash", n)
                    if #card > 0 then
                        room:moveCards({
                            ids = card,
                            to = player,
                            toArea = Card.PlayerHand,
                            moveReason = fk.ReasonJustMove,
                            proposer = player,
                            skillName = skillName,
                        })
                    end
                    room:addPlayerMark(player, MarkEnum.SlashResidue .. "-turn", n)
                elseif num == 8 then
                    for _ = 1, n, 1 do
                        local cards = table.filter(U.getUniversalCards(room, "bt"), function(id)
                            return Fk:getCardById(id).is_damage_card
                        end)
                        local use = room:askToUseRealCard(player, {
                            pattern = cards,
                            skill_name = skillName,
                            prompt = "祈星：请选择需要使用的牌",
                            extra_data = {
                                bypass_times = true,
                                expand_pile = cards,
                            },
                            skip = true,
                        })
                        if use then
                            local card = Fk:cloneCard(use.card.name)
                            card.skillName = skillName
                            use = {
                                card = card,
                                from = player,
                                tos = use.tos,
                                extraUse = true,
                            }
                            room:useCard(use)
                        end
                    end
                elseif num == 9 then
                    local blackgenerallist = { "que__xuanyin" }
                    local generals, same_generals = {}, {}
                    for _, general_name in ipairs(room.general_pile) do
                        same_generals = Fk:getSameGenerals(general_name)
                        table.insert(same_generals, general_name)
                        same_generals = table.filter(same_generals, function(g_name)
                            local general = Fk.generals[g_name]
                            return general.package.extensionName == "MahjongSoul" and
                                not table.contains(blackgenerallist, general.name)
                        end)
                        if #same_generals > 0 then
                            table.insert(generals, table.random(same_generals))
                        end
                    end
                    if #generals == 0 then return false end
                    generals = table.random(generals, 3)

                    local skills = {}
                    for _, general_name in ipairs(generals) do
                        local general = Fk.generals[general_name]
                        local general_skills = {}
                        for _, s in ipairs(general:getSkillNameList()) do
                            local skill = Fk.skills[s]
                            if not player:hasSkill(skill) and not (skill:hasTag(Skill.AttachedKingdom) and not table.contains(skill:getSkeleton().attached_kingdom, player.kingdom)) then
                                table.insertIfNeed(general_skills, s)
                            end
                        end
                        table.insertIfNeed(skills, general_skills)
                    end
                    if #skills > 0 then
                        local choices = U.askToChooseGeneralSkills(player, {
                            generals = generals,
                            skills = skills,
                            min_num = n,
                            max_num = n,
                            skill_name = skillName,
                            prompt = " 祈星：请选择一个技能获得",
                            cancelable = false
                        })
                        room:handleAddLoseSkills(player, table.concat(choices, "|"))
                        room:addPlayerMark(player, "qiyue_skill_add", n)
                    end
                end
            else
                local num = math.random(1, 8)
                local n = 1 + player:getMark("que_zheyue_num")
                local to
                if num == 2 or num == 4 or num == 1 or num == 8 or num == 6 then
                    local prompt = ""
                    if num == 2 then
                        prompt = "祈月：选择一名角色失去 " .. n .. " 点体力"
                    elseif num == 4 then
                        prompt = "祈月：选择一名角色增加 " .. n .. " 点体力上限"
                    elseif num == 1 then
                        prompt = "祈月：选择一名角色，弃置其 " .. n .. " 张牌，若其没有牌，你摸" .. n .. "张牌"
                    elseif num == 8 then
                        prompt = "祈月：选择一名角色，视为对其使用 " .. n .. " 张伤害牌"
                    elseif num == 6 then
                        prompt = "祈月：选择一名角色，令其获得牌堆的底的" .. n .. "张牌，然后弃置其一张判定牌，若没有判定牌，其额外获得牌堆的顶牌"
                    end
                    local tos = room:askToChoosePlayers(player, {
                        skill_name = skillName,
                        min_num = 1,
                        max_num = 1,
                        targets = room.alive_players,
                        prompt = prompt,
                        cancelable = true,
                    })
                    if #tos == 0 then return end
                    to = tos[1]
                end
                if player.dead then return end
                if num == 1 then
                    player:drawCards(n, skillName)
                    if #to:getCardIds { Player.Hand, Player.Equip, Player.Judge } > 0 then
                        local cards = room:askToChooseCards(player,
                            {
                                min = 1,
                                max = n,
                                flag = "he",
                                skill_name = skillName,
                                target = to,
                            }
                        )
                        room:throwCard(cards, skillName, to, player)
                    else
                        player:drawCards(n, skillName)
                    end
                elseif num == 2 then
                    room:loseHp(to, n, skillName)
                elseif num == 3 then
                    room:addPlayerMark(player, "@que_qixing_damagedown", 1)
                elseif num == 4 then
                    room:changeMaxHp(to, n)
                elseif num == 5 then
                    room:addPlayerMark(player, "@que_qixing_useup", 1)
                elseif num == 6 then
                    to:drawCards(n, skillName, "top")
                    if #to:getCardIds(Player.Judge) > 0 then
                        local cards = room:askToChooseCards(player,
                            {
                                min = 1,
                                max = n,
                                flag = "j",
                                skill_name = skillName,
                                target = to,
                            }
                        )
                        room:throwCard(cards, skillName, to, player)
                    else
                        to:drawCards(n, skillName, "bottom")
                    end
                elseif num == 7 then
                    local card = room:getCardsFromPileByRule("slash", n)
                    if #card > 0 then
                        room:moveCards({
                            ids = card,
                            to = player,
                            toArea = Card.PlayerHand,
                            moveReason = fk.ReasonJustMove,
                            proposer = player,
                            skillName = skillName,
                        })
                    end
                    room:addPlayerMark(player, MarkEnum.SlashResidue .. "-turn", n)
                elseif num == 8 then
                    for i = 1, n, 1 do
                        local cards = table.filter(U.getUniversalCards(room, "bt"), function(id)
                            return Fk:getCardById(id).is_damage_card
                        end)
                        local use = room:askToUseRealCard(player, {
                            pattern = cards,
                            skill_name = skillName,
                            prompt = "祈星：请选择需要使用的牌",
                            extra_data = {
                                bypass_times = true,
                                expand_pile = cards,
                            },
                            skip = true,
                        })
                        if use then
                            local card = Fk:cloneCard(use.card.name)
                            card.skillName = skillName
                            use = {
                                card = card,
                                from = player,
                                tos = use.tos,
                                extraUse = true,
                            }
                            room:useCard(use)
                        end
                    end
                end
            end
        end
    end,
})

que_qixing:addEffect(fk.DamageInflicted, {
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(que_qixing.name) and player:getMark("@que_qixing_damagedown") > 0
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local n = 1 + player:getMark("que_zheyue_num")
        return room:askToSkillInvoke(player, {
            skill_name = que_qixing.name,
            prompt = "祈星：是否回复" .. n .. "点体力？"
        })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        player:broadcastSkillInvoke("que_qixing")
        local n = 1 + player:getMark("que_zheyue_num")
        room:removePlayerMark(player, "@que_qixing_damagedown", 1)
        room:recover { num = n, skillName = que_qixing.name, who = player, recoverBy = player }
    end,
})

que_qixing:addEffect(fk.CardUsing, {
    can_trigger = function(self, event, target, player, data)
        local cards = { "jink", "nullification" }
        return target == player and player:hasSkill(que_qixing.name) and player:getMark("@que_qixing_useup") > 0 and
            (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and
            not table.contains(cards, data.card.trueName)
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local n = 1 + player:getMark("que_zheyue_num")
        return room:askToSkillInvoke(player, {
            skill_name = que_qixing.name,
            prompt = "祈星：是否令" .. data.card:toLogString() .. "额外结算" .. n .. "次？"
        })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        player:broadcastSkillInvoke("que_qixing")
        local n = 1 + player:getMark("que_zheyue_num")
        room:removePlayerMark(player, "@que_qixing_useup", 1)
        data.additionalEffect = (data.additionalEffect or 0) + n
    end,
})

return que_qixing
